package com.example.httptest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.ResponseCache;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.JsonElement;

import android.app.Activity;
import android.content.Entity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	TextView tv;
	public static final int UPDATE_TEXT = 1;
	public Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:

				String response = (String) msg.obj;
				tv.setText(response);
				break;
			case 0:
				Toast.makeText(MainActivity.this, "fail", Toast.LENGTH_SHORT)
						.show();

			default:
				break;
			}
		};
	};

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		tv = (TextView) findViewById(R.id.json);
		new Thread(new Runnable() {

			public void run() {
				String url = "http://123.57.221.116:8080/light-server/intf/user/login.shtml";
				HttpClient httpClient = new DefaultHttpClient();
				try {
					HttpPost httpPost = new HttpPost(url);
					HttpParams httpParams = new BasicHttpParams();
					List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
					Gson gson = new Gson();
					String str = gson.toJson(initData());
					nameValuePair.add(new BasicNameValuePair("jsonString",
							URLEncoder.encode(str, "utf-8")));
					httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
					httpPost.setParams(httpParams);
					httpClient.execute(httpPost);
					HttpResponse httpResponse = httpClient.execute(httpPost);
					StatusLine statusLine = httpResponse.getStatusLine();
					if (statusLine != null
							&& (statusLine.getStatusCode()) == 200) {
						HttpEntity entity = httpResponse.getEntity();
						Message message = Message.obtain();
						String response;
						if (entity != null) {
							message.what = 1;
							response = readInputStream(entity.getContent());
							message.obj = response;

						} else {
							message.what = 0;
							response = null;
						}

						handler.sendMessage(message);
					}
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}

	private static String readInputStream(InputStream is) throws IOException {
		if (is == null)
			return null;
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		int len = 0;
		byte[] buf = new byte[1024];
		while ((len = is.read(buf)) != -1) {
			bout.write(buf, 0, len);
		}
		is.close();
		return URLDecoder.decode(new String(bout.toByteArray()), "utf-8");
	}

	public List<User> initData() {

		List<User> users = new ArrayList<User>();

		User p = new User();
		p.setCode("13312345678");
		p.setPwd("123456");
		users.add(p);

		return users;

	}
}
